<?php

declare(strict_types=1);

namespace App\Service;

use App\Common\Tools\DbTools;
use App\Library\Common;
use App\Model\FlyShopAd;
use App\Model\ReservedSeatsSet;
Use Hyperf\Di\Annotation\Inject;
use App\Service\ProductService;
use Hyperf\DbConnection\Db;

class FlyShopAdService extends BaseService
{
    /**
     * 审核
     * @Inject
     * @var ProductService
     */
    private $productService;

    public function info(int $id)
    {
        return ReservedSeatsSet::query()
            ->where('shop_id',$id)
            ->first();
    }

    public function index($param,$shop_id='')
    {
        $where = [];
        if($shop_id!=''){
            $where['shop_id'] = $shop_id ;
        }
        $where['is_show'] = 1;
        if(isset($param['is_show'])&&$param['is_show']!==''){
            $where['is_show'] = $param['is_show'] ;
        }
        return FlyShopAd::query()
            ->where($where)
            ->with('product_info')
            ->with('commissions')
            ->withCount('commissions as commissions_num')
            ;
    }

    /**
     * 设置
     * @param int $shop_id
     * @param array $param
     * @return bool|int
     */
    public function add(int $shop_id,array $param)
    {
        if(isset($param['product_ids'])&&$param['product_ids']!=[]){
            $product_ids = $param['product_ids'];
            $arr = [];
            foreach ($product_ids as $value){
                $this->productService->get_best_price(intval($value),1);
                $arr[] = [
                  'shop_id'=>  $shop_id,
                  'product_id'=>  $value,
                  'radio'=>  $param['radio'],
                  'created_at'=>  $param['created_at'],
                  'create_by'=>  $param['create_by'],
                  'min_price'=>   $this->productService->get_best_price(intval($value),1),
                  'max_price'=>   $this->productService->get_best_price(intval($value),2),
                ];
            }
            return FlyShopAd::query()->insert($arr);
        }else{
            return false;
        }
    }

    /**
     * 设置推广修改
     * @param array $param
     * @return bool|int
     */
    public function edit(array $param)
    {
        $param['min_price'] = $this->productService->get_best_price(intval($param['product_id']),1);
        $param['max_price'] = $this->productService->get_best_price(intval($param['product_id']),2);
        return FlyShopAd::query()->where('id',$param['id'])->update($param);
    }
}